<template>
{{#if ctrl.psSysPFPlugin}}
  {{> @macro/plugins/widget/widget-use.hbs appPlugin=ctrl.psSysPFPlugin}}
{{else}}
  <div :class="classNames" :id="controlID"{{#or ctrl.width ctrl.height}} style="{{#if ctrl.width}}width: {{ctrl.width}}px;{{/if}}{{#if ctrl.height}}width: {{ctrl.height}}px;{{/if}}"{{/or}}>
    <a-form class="control-content">
      <AppGrid
        :columns="model.columns"
        :aggData="store.aggData"
        :items="store.data"
        :singleSelect="singleSelect"
        :selections="store.selections"
        :showHeader="model.showHeader"
        :current="store.curPage"
        :limit="store.limit"
        :totalRecord="store.totalRecord"
        :context="store.context"
        :viewParams="store.viewParams"
        :enableRowEdit="model.enableRowEdit"
        :enableAgg="model.aggMode !== 'NONE'"
  {{#if ctrl.enablePagingBar}}
        :paging="model.paging"
  {{/if}}
  {{#if ctrl.emptyText}}
        emptyText="{{ctrl.emptyText}}"
  {{/if}}
  {{#if ctrl.emptyTextPSLanguageRes}}
        emptyTextPSLanguageRes="{{ctrl.emptyTextPSLanguageRes}}"
  {{/if}}
        @row-click="handleRowClick"
        @row-db-click="handleRowDbClick"
        @selection-change="handleSelectionChange"
  {{#unless ctrl.noSort}}
        @sort-change="(field: string, dir: '' | 'asc' | 'desc') => handleSortChange(controller, field, dir)"
  {{/unless}}
  {{#if ctrl.enablePagingBar}}
        @page-change="(currentPage: number) => handlePageChange(controller, currentPage)"
        @page-size-change="(pageSize: number) => handlePageSizeChange(controller, pageSize)"
  {{/if}}
        @action-click="(logic: any, data: IParam, event: MouseEvent) => handleActionClick(controller, logic as IViewLogicInput, data, event)"
      >
  {{#if ctrl.enableRowEdit}}
    <template #header="{ title, column }">
      <AppGridColumnHeader :column="column" :context="context" :viewParams="viewParams" />
    </template>
    <template #body="{ column, text, record, index }">
    {{#each ctrl.psDEGridColumns as | column |}}
      {{> @macro/widgets/grid-detail/include-grid.hbs type='EDITCOLUMN' column=column grid=ctrl}}
    {{/each}}
    </template>
  {{/if}}
  {{#each ctrl.psDEGridColumns as | column |}}
  {{#if column.psSysPFPlugin}}
        <template #{{column.psSysPFPlugin.pluginCode}}="{ rowData }">
        {{> @macro/plugins/ctrl-item/ctrl-item-use.hbs ctrl=ctrl appPlugin=column.psSysPFPlugin}}
        </template>
  {{/if}}
  {{/each}}
      </AppGrid>
    </a-form>
  </div>
{{/if}}
</template>
<script setup lang="ts">
// 基于template/src/widgets/\{{appEntities}}/\{{ctrls@GRID}}-grid/\{{spinalCase ctrl.codeName}}-grid.vue.hbs生成
{{> @macro/plugins/widget/widget-import.hbs ctrl=ctrl}}
{{> @macro/plugins/ctrl-item/ctrl-item-import.hbs ctrl=ctrl}}
import { AppGrid } from "@components/widgets/grid";
import { IContext, IParam, IViewLogicInput } from "@/core";
import { GridActionType, ICtrlDataAction, IGridAbility, IGridControllerParams, IGridStore, GridController, ILoadingHelper, LoadingHelper, createUUID, IViewCtx } from "@/core";
{{#or ctrl.enablePagingBar (neq ctrl.noSort true)}}
import { {{#if ctrl.enablePagingBar}}handlePageChange, handlePageSizeChange, {{/if}}{{#unless ctrl.noSort}}handleSortChange, {{/unless}}handleComponentAction } from '@/hooks/use-ctrl';
{{/or}}
{{#if ctrl.enableRowEdit}}
import { AppGridColumn, AppGridColumnHeader } from '@components/widgets/grid';
{{/if}}
import { useNavParamsBind, useEventBind, handleActionClick, getCtrlClassNames } from '@/hooks/use-ctrl';
import { {{pascalCase ctrl.name}}ControlVO } from './{{spinalCase ctrl.codeName}}-grid-vo';
import { model } from "./{{spinalCase ctrl.codeName}}-grid-model";
import GridService from "@/core/modules/ctrl-service/grid-service";
{{> @macro/widgets/ctrl/ctrl-props.hbs
    props="openView?: Function;
    newView?: Function;
    actions: ICtrlDataAction;
    singleSelect?: boolean;
    selectFirstDefault?: boolean;
    rowActiveMode?: 0 | 1 | 2;
    defaultEnableRowEdit?: boolean;
    "
    propsDefault="singleSelect: false,
    rowActiveMode: 2
    "
}}

{{> @macro/common/emit.hbs name="ctrl" actionType="GridActionType" ability="IGridAbility"}}

//  部件样式名
const classNames = computed(() => {
  const names = getCtrlClassNames(model, props);
  Object.assign(names, {
    "app-control-grid--no-paging-bar": model.enablePagingBar,
  });
  return names;
});

const controlID = createUUID();
const ctrlService = new GridService<{{pascalCase ctrl.name}}ControlVO>({{pascalCase ctrl.name}}ControlVO, model.entityCodeName);
const params: IGridControllerParams<GridActionType, IGridAbility> = {
  name: props.name,
  model,
  evt,
  controlID,
  pLoadingHelper: props.pLoadingHelper,
  defaultEnableRowEdit: props.defaultEnableRowEdit,
  actions: props.actions,
  rowActiveMode: props.rowActiveMode,
  singleSelect: props.singleSelect,
  selectFirstDefault: props.selectFirstDefault,
  ctrlService: ctrlService,
  openView: props.openView,
  newView: props.newView,
  closeView: props.closeView,
  pViewCtx: props.pViewCtx,
  handler: (data: IGridStore) => { return reactive(data) }
};
//  表格控制器
const controller = new GridController(params);
useNavParamsBind(controller, props);
const store: IGridStore = controller.getStore();

const handleRowClick = (row: IParam, event: MouseEvent) => {
  controller.rowClick(row, event);
}

const handleRowDbClick = (row: IParam, event: MouseEvent) => {
  controller.rowDbClick(row, event);
}

const handleSelectionChange = (selections: IParam[]) => {
  controller.selectionChange(selections);
}

onMounted(() => {
  controller.ctrlMounted();
});
onUnmounted(() => {
  controller.ctrlDestroy();
})
</script>
